﻿using SanXiao.Game.Common;
using SanXiao.Game.GameCenter.Bubbles;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SanXiao.Game.GameCenter.Roles
{
    public abstract class RoleObject : IDisposable
    {
        const int defaultW = 418;
        const int defaultH = 566;
        private readonly ImageObject[][] _images;
        public RoleObject(ImageObject[][] images, RoleType roleType)
        {
            _images = images;
            RoleType = roleType;
        }
        public RoleType RoleType { get; private set; }
        public ImageObject[][] Images => _images;
        private int _width;
        public int Width
        {
            get => _width;
            set
            {
                _width = value;
                if (_defaultWidth == 0)
                    _defaultWidth = value;
            }
        }
        private int _height;
        public int Height
        {
            get=> _height;
            set
            {
                _height = value;
                if(_defaultHeight == 0)
                    _defaultHeight = value;
            }
        }
        private int _x;
        public int X
        {
            get => _x;
            set
            {
                _x = value;
                if (_defaultX == 0)
                    _defaultX = value;
            }
        }
        private int _y;
        public int Y
        {
            get=>_y;
            set
            {
                _y = value;
                if(_defaultY==0)
                    _defaultY = value;
            }
        }
        public float ZoomX { get; set; } = 1f;
        public double ZoomY { get; set; } = 1f;
        public BubbleObject Bubble { get; set; }
        public RoleAction RoleAction { get; set; } = RoleAction.Stand;
        private int _defaultX;
        private int _defaultY;
        private int _defaultWidth;
        private int _defaultHeight;
        public void SizeChange(float zoomX,float zoomY)
        {
            this.ZoomX = zoomX;
            this.ZoomY = zoomY;

            this.X = (int)(_defaultX * zoomX);
            this.Y = (int)(_defaultY * zoomY);
            this.Width = (int)(_defaultWidth * zoomX);
            this.Height = (int)(_defaultHeight * zoomY);
        }
        public void SetAction(RoleAction roleAction)
        {
            RoleAction = roleAction;
            _index = 0;
        }
        private int _index;
        public virtual void Draw(Graphics g)
        {
            var arr = _images[RoleAction.GetHashCode()];
            var image = arr[_index];
            IntPtr hdc=g.GetHdc();
            GDI32.DrawImageTransparenc(hdc, image.BitmapPtr, X, Y, image.Width, image.Height, this.Width, this.Height);
            g.ReleaseHdc(hdc);
            if (++_index >= arr.Length)
            {
                RandomRoleAction();
            }
            if (Bubble != null)
            {
                Bubble.X = X+(this.Width>>1); 
                Bubble.Y = Y;
                Bubble.Draw(g);
            }
        }
        readonly Random _random = new Random();
        protected virtual void RandomRoleAction()
        {
            var next = _random.Next(100);
            RoleAction ra = RoleAction.Stand;
            if (next >= 80)
                ra = RoleAction.Stand;
            else if (next > 65)
                ra = RoleAction.Action;
            else if (next >= 50)
                ra = RoleAction.Happy;
            else if (next >= 30)
                ra = RoleAction.Joy;
            else if (next >= 20)
                ra = RoleAction.Sad;
            else if (next >= 10)
                ra = RoleAction.Surpris;
            else
                ra = RoleAction.Uneasy;

            SetAction(ra);
        }
        public static ImageObject[] ToImageObjects(Bitmap[] bitmaps)
        {
            ImageObject[] images = new ImageObject[bitmaps.Length];
            for (int i = 0; i < images.Length; i++)
            {
                images[i] = new ImageObject(bitmaps[i]);
            }
            return images;
        }
        public void Dispose()
        {
            for (int i = 0; i < _images.Length; i++)
            {
                var image = _images[i];
                for (int j = 0; j < image.Length; j++)
                {
                    var item = image[i];
                    item.Dispose();
                }
            }
        }
    }
}
